/// 生成和保存图像
///
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkPNGWriter.h>

/// 生成400x400的图，半径100的圆
void createImage(vtkImageData* image)
{
    unsigned int dim = 400;
    double r = 100.0f;

    image->SetDimensions(dim, dim, 1);
    image->AllocateScalars(VTK_UNSIGNED_CHAR,1);

    double r2 = r*r;
    for(unsigned int x = 0; x < dim; x++)
    {
        for(unsigned int y = 0; y < dim; y++)
        {
            unsigned char* pixel = static_cast<unsigned char*>(image->GetScalarPointer(x,y,0));
            if ((x-dim/2)*(x-dim/2) + (y-dim/2)*(y-dim/2) < r2) {
                pixel[0] = 200;
            } else {
                pixel[0] = 30;
            }
        }
    }

    image->Modified();
}


int main(int argc, char *argv[])
{
    std::string outputFilename = "output.png";

    // Create an image
    vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New();
    createImage(image);

    // 保存图像
    vtkSmartPointer<vtkPNGWriter> writer = vtkSmartPointer<vtkPNGWriter>::New();
    writer->SetFileName( outputFilename.c_str() );
    writer->SetInputData(image);
    writer->Write();

    return EXIT_SUCCESS;
}
